﻿using RoadQualityEvalAPI.Helpers;
using RoadQualityEvalAPI.Models;
using RoadQualityEvaluatorData.Domain.Entities;
using RoadQualityEvaluatorData.DomainServices.Generic;
using RoadQualityEvaluatorData.DomainServices.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace RoadQualityEvalAPI.Controllers
{
    public class SpeedbumpController : ApiController
    {

        private SpeedbumpRepository _speedbumpRepository;

        private UserRepository _userRepository;


        public List<SpeedbumpModel> GetAllSpeedbumps()
        {
            var speedbumps = new List<SpeedbumpModel>();

            _speedbumpRepository = new SpeedbumpRepository();

            if (_speedbumpRepository.GetAll().Count > 0)
            {
                foreach (var sb in _speedbumpRepository.GetAll())
                {
                    speedbumps.Add(new SpeedbumpModel()
                    {
                        CreatedAt = sb.CreatedAt,
                        Description = sb.Description,
                        Location = new LocationModel() { Latitude = sb.Location.Latitude, Longitude = sb.Location.Longitude }
                    });
                }
            }

            return speedbumps;
        }

        public List<SpeedbumpModel> GetAllSpeedbumpsFromUser(string username)
        {
            var speedbumps = new List<SpeedbumpModel>();

            UnitOfWork uo = new UnitOfWork();
            _speedbumpRepository = new SpeedbumpRepository(uo);
            _userRepository = new UserRepository(uo);

            var userWhoCollected = _userRepository.GetUserByUsername(username);

            if (_speedbumpRepository.GetAll().Where(sb => sb.UserWhoCollected.Id  == userWhoCollected.Id).Count() > 0)
            {
                foreach (var sb in _speedbumpRepository.GetAll().Where(sb => sb.UserWhoCollected.Id  == userWhoCollected.Id))
                {
                    speedbumps.Add(new SpeedbumpModel()
                    {
                        CreatedAt = sb.CreatedAt,
                        Description = sb.Description,
                        Location = new LocationModel() { Latitude = sb.Location.Latitude, Longitude = sb.Location.Longitude }
                    });
                }
            }

            return speedbumps;
        }


        public SpeedbumpModel GetSpeedbump(int id)
        {
            _speedbumpRepository = new SpeedbumpRepository();

            var s = _speedbumpRepository.GetById(id);

            if (s == null)
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));


            return new SpeedbumpModel() { Location = new LocationModel() { Latitude = s.Location.Latitude, Longitude = s.Location.Longitude }, Description = s.Description, CreatedAt = s.CreatedAt };
        }

        public SpeedbumpModel GetSpeedbumpByLocation(double latitude, double longitude)
        {
            _speedbumpRepository = new SpeedbumpRepository();

            var s = _speedbumpRepository.GetAll().FirstOrDefault(sb => sb.Location.Latitude == latitude && sb.Location.Longitude == longitude);

            if (s == null)
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));

            return new SpeedbumpModel() { CreatedAt = s.CreatedAt, Description = s.Description, Location = new LocationModel() { Latitude = s.Location.Latitude, Longitude = s.Location.Longitude } };
        }


        [HttpPost]
        public void AddSpeedbump(SpeedbumpModel speedbump)
        {
            _speedbumpRepository = new SpeedbumpRepository();

            if (ModelState.IsValid)
            {
                _speedbumpRepository.Add(new Speedbump()
                {
                    CreatedAt = speedbump.CreatedAt,
                    Description = speedbump.Description,
                    Location = new Location() { Latitude = speedbump.Location.Latitude, Longitude = speedbump.Location.Longitude }
                });

            }
        }


        [HttpPost]
        public void AddSpeedbumpList(List<SpeedbumpModel> speedbumps)
        {
            UnitOfWork uo = new UnitOfWork();

            _speedbumpRepository = new SpeedbumpRepository(uo);

            _userRepository = new UserRepository(uo);

            SpeedbumpHelper sbHelper = new SpeedbumpHelper(uo);

            if (speedbumps != null && speedbumps.Count > 0)
            {
                var userWhoCollected = _userRepository.GetUserByUsername(speedbumps.First().Username);

                foreach (var speedbump in speedbumps)
                {
                    Speedbump x = new Speedbump()
                    {
                        CreatedAt = speedbump.CreatedAt,
                        Description = speedbump.Description,
                        Location = new Location() { Latitude = speedbump.Location.Latitude, Longitude = speedbump.Location.Longitude },
                        UserWhoCollected = userWhoCollected
                    };

                    // check if speedbump already exists
                    var closestExistingSb = sbHelper.CheckIfSpeedbumpAlreadyExists(x);
                    if (closestExistingSb == null)
                    {
                        _speedbumpRepository.Add(x);

                        sbHelper.AssignSpeedbumpToMapSection(x);
                    }
                    // TODO: implement quality attribute in speedbump entities
                    /*
                    else
                    {
                        closestExistingSb.
                    }
                    */
                    
                }
            }
        }



    }
}
